{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Developer Tutorial\n",
    "\n",
    "This is a tutorial for anybody who wants to either develop LFortran or build\n",
    "tools on top.\n",
    "\n",
    "## Introduction\n",
    "\n",
    "LFortran is structured around two independent modules, AST and ASR, both of\n",
    "which are standalone (completely independent of the rest of LFortran) and users\n",
    "are encouraged to use them independently for other applications and build tools\n",
    "on top:\n",
    "\n",
    "* Abstract Syntax Tree (AST): Represents any Fortran\n",
    "  source code, strictly based on syntax, no semantic is included. The AST\n",
    "  module can convert itself to Fortran source code.\n",
    "\n",
    "* Abstract Semantic Representation (ASR): Represents a\n",
    "  valid Fortran source code, all semantic is included. Invalid Fortran code is\n",
    "  not allowed (an error will be given). The ASR module can convert itself to an\n",
    "  AST.\n",
    "\n",
    "## Abstract Syntax Tree (AST)\n",
    "\n",
    "Fortran source code can be parsed into an AST using the `src_to_ast()`\n",
    "function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integer function f(a, b) result(r)\n",
    "integer, intent(in) :: a, b\n",
    "r = a + b\n",
    "end function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can pretty print it using the `%%showast` magic:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%showast\n",
    "integer function f(a, b) result(r)\n",
    "integer, intent(in) :: a, b\n",
    "r = a + b\n",
    "end function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can convert AST to Fortran source code using `%%showfmt`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%showfmt\n",
    "integer function f(a, b) result(r)\n",
    "integer, intent(in) :: a, b\n",
    "r = a + b\n",
    "end function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All AST nodes and their arguments are described in\n",
    "[AST.asdl](https://gitlab.com/lfortran/lfortran/blob/master/grammar/AST.asdl).\n",
    "\n",
    "## Abstract Semantic Representation (ASR)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can pretty print using the `%%showasr` magic:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%showasr\n",
    "integer function f(a, b) result(r)\n",
    "integer, intent(in) :: a, b\n",
    "r = a + b\n",
    "end function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All ASR nodes and their arguments are described in\n",
    "[ASR.asdl](https://gitlab.com/lfortran/lfortran/blob/master/src/libasr/ASR.asdl)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Fortran",
   "language": "fortran",
   "name": "fortran"
  },
  "language_info": {
   "file_extension": ".f90",
   "mimetype": "text/x-fortran",
   "name": "fortran",
   "version": "2018"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
